﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.Windows.Threading;

namespace Ricciolo.StylesExplorer.Model
{
    class AsyncObservableCollection<T> : ObservableCollection<T>, IAsyncLoad
    {
        private bool _isLoading;
        private ISynchronizeInvoke invoker;

        public AsyncObservableCollection(ISynchronizeInvoke invoker)
        {
            this.invoker = invoker;
        }

        public AsyncObservableCollection()
        {
        }

        public bool IsLoading
        {
            get { return _isLoading; }
            private set
            {
                if (_isLoading != value)
                {
                    _isLoading = value;
                    OnPropertyChanged("IsLoading");
                }
            }
        }

        private void OnPropertyChanged(string propertyName)
        {
            this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        public void RaiseAdded()
        {
            // Invoke events on main thread
            this.invoker.BeginInvoke(new MethodInvoker(delegate
            {
                base.OnPropertyChanged(new PropertyChangedEventArgs("Count"));
                base.OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
                base.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, 0));
            }), null);
        }

    }
}
